package com.dap.cms.governance.service.impl;

import com.dap.cms.base.impl.BaseServiceImpl;
import com.dap.cms.governance.constant.ExcutorConstant;
import com.dap.cms.governance.po.*;
import com.dap.cms.governance.service.*;
import com.dap.cms.governance.vo.UserInfoVo;
import com.dap.dao.model.QueryResult;
import com.fesine.commons.util.CryptographyUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description: t_cms_user表对应的服务接口实现类
 * @author: Fesine
 * @createTime:2018/08/20
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2018/08/20
 */
@Service
public class CmsUserServiceImpl extends BaseServiceImpl<CmsUserPo>
        implements CmsUserService {
    @Autowired
    private CmsManagerService cmsManagerService;
    @Autowired
    private CmsUserService cmsUserService;
    @Autowired
    private CmsComponentManagerService cmsComponentManagerService;
    @Autowired
    private CmsComponentUserService cmsComponentUserService;
    @Autowired
    private CmsCompanyService cmsCompanyService;
    @Autowired
    private CmsSysService cmsSysService;

    @Override
    public void deleteUser(CmsUserPo cmsUserPo) {
        //删除用户同时删除manager表中的记录
        CmsManagerPo cmsManagerPo = new CmsManagerPo();
        cmsManagerPo.setUsername(cmsUserPo.getName());
        cmsManagerPo = cmsManagerService.get(cmsManagerPo);
        CmsComponentUserPo cmsComponentUserPo = new CmsComponentUserPo();
        cmsComponentUserPo.setUserId(cmsUserPo.getId());
        //删除user组件关联表中的记录
        cmsComponentUserService.delete(cmsComponentUserPo);
        CmsComponentManagerPo cmsComponentManagerPo = new CmsComponentManagerPo();
        if (null != cmsManagerPo) {
            //删除manager组件关联表中的记录
            cmsComponentManagerPo.setManagerId(cmsManagerPo.getId());
            cmsComponentManagerService.delete(cmsComponentManagerPo);
            cmsManagerService.delete(cmsManagerPo);
        }
        delete(cmsUserPo);
    }

    @Override
    public void addCmsUserPo(CmsUserPo cmsUserPo) {
        save(cmsUserPo);
        //为公司用户创建登录账号
        CmsManagerPo cmsManagerPo = new CmsManagerPo();
        cmsManagerPo.setUsername(cmsUserPo.getName());
        //普通开发者不创建登录账号
        if (cmsUserPo.getUserFlag() != 0) {
            cmsManagerPo.setManagerFlag(cmsUserPo.getUserFlag());
        }
        cmsManagerPo.setPassword(CryptographyUtil.md5("123456", "cms"));
        cmsManagerService.save(cmsManagerPo);
    }

    @Override
    public Boolean checkName(String name) {
        CmsUserPo userPo = new CmsUserPo();
        userPo.setName(name);
        List<CmsUserPo> cmsUserPos = listAll(userPo);
        CmsManagerPo cmsManagerPo = new CmsManagerPo();
        cmsManagerPo.setUsername(name);
        List<CmsManagerPo> cmsManagerPos = cmsManagerService.listAll(cmsManagerPo);
        return CollectionUtils.isEmpty(cmsUserPos) && CollectionUtils.isEmpty(cmsManagerPos);
    }

    @Override
    public void addCmsUserPoAndComponent(CmsUserPo cmsUserPo, String componentIds) {
        save(cmsUserPo);
        int userId = cmsUserPo.getId();
        CmsManagerPo cmsManagerPo = new CmsManagerPo();
        cmsManagerPo.setUsername(cmsUserPo.getName());
        cmsManagerPo.setManagerFlag(cmsUserPo.getUserFlag());
        cmsManagerPo.setPassword(CryptographyUtil.md5("123456", "cms"));
        cmsManagerService.save(cmsManagerPo);
        int managerId = cmsManagerPo.getId();
        String[] idArray = componentIds.split(",");
        for (int i = 0; i < idArray.length; i++) {
            Integer componentId = Integer.parseInt(idArray[i]);
            //在组件——管理员中间表中加入记录
            CmsComponentManagerPo cmsComponentManagerPo = new CmsComponentManagerPo();
            cmsComponentManagerPo.setManagerId(managerId);
            cmsComponentManagerPo.setComponentId(componentId);
            cmsComponentManagerService.save(cmsComponentManagerPo);
            //在组件--用户表中加入记录
            CmsComponentUserPo cmsComponentUserPo = new CmsComponentUserPo();
            cmsComponentUserPo.setUserId(userId);
            cmsComponentUserPo.setComponentId(componentId);
            cmsComponentUserService.save(cmsComponentUserPo);
        }
    }

    @Override
    public void register(String name, String telephone, String companyName, String sysName, String password) {
        CmsCompanyPo cmsCompanyPo = new CmsCompanyPo();
        cmsCompanyPo.setCompanyName(companyName);
        CmsCompanyPo companyPo = cmsCompanyService.get(cmsCompanyPo);
        int companyId;
        if (companyPo == null) {
            cmsCompanyService.save(cmsCompanyPo);
            companyId = cmsCompanyPo.getId();
        } else {
            companyId = companyPo.getId();
        }
        CmsSysPo cmsSysPo = new CmsSysPo();
        cmsSysPo.setSysName(sysName);
        cmsSysPo.setCompanyId(companyId);
        cmsSysService.save(cmsSysPo);
        Integer sysId = cmsSysPo.getId();
        CmsUserPo cmsUserPo = new CmsUserPo();
        cmsUserPo.setName(name);
        cmsUserPo.setSysId(sysId);
        cmsUserPo.setCompanyId(companyId);
        cmsUserPo.setTelephone(telephone);
        //注册的角色为公司系统负责人
        cmsUserPo.setUserFlag(2);
        cmsUserService.save(cmsUserPo);
        //生成登录账户
        CmsManagerPo cmsManagerPo = new CmsManagerPo();
        cmsManagerPo.setUsername(name);
        cmsManagerPo.setManagerFlag(2);
        cmsManagerPo.setPassword(CryptographyUtil.md5(password, "cms"));
        cmsManagerService.save(cmsManagerPo);
    }

    @Override
    public QueryResult<UserInfoVo> listUserInfo(UserInfoVo userInfoVo, Integer pageIndex, Integer pageSize) {
        return daoService.selectQueryResult(CmsUserPo.class.getName() + ".listUserInfo", userInfoVo, pageIndex, pageSize);
    }

    @Override
    public void updateUser(CmsUserPo cmsUserPo, String componentIds,String oldUserName) {
        //更新userPo表
        update(cmsUserPo);
        //删除对应ComponentUser表记录
        CmsComponentUserPo cmsComponentUserPo=new CmsComponentUserPo();
        cmsComponentUserPo.setUserId(cmsUserPo.getId());
        cmsComponentUserService.delete(cmsComponentUserPo);
        //删除manager表记录
        CmsManagerPo cmsManagerPo=new CmsManagerPo();
        cmsManagerPo.setUsername(oldUserName);
        cmsManagerPo=cmsManagerService.get(cmsManagerPo);
        Integer managerId = cmsManagerPo.getId();
        cmsManagerService.delete(cmsManagerPo);
        //生成新的manager表记录
        cmsManagerPo.setId(managerId);
        cmsManagerPo.setUsername(cmsUserPo.getName());
        cmsManagerPo.setManagerFlag(cmsUserPo.getUserFlag());
        cmsManagerPo.setPassword(CryptographyUtil.md5("123456", "cms"));
        cmsManagerService.save(cmsManagerPo);
        //删除ComponentManager表中对应的记录
        CmsComponentManagerPo cmsComponentManagerPo=new CmsComponentManagerPo();
        cmsComponentManagerPo.setManagerId(managerId);
        cmsComponentManagerService.delete(cmsComponentManagerPo);
        //如果修改为组件接入员需重新添加关联组件
        if(ExcutorConstant.SYS_COMPONENT_MANAGER.equals(cmsUserPo.getUserFlag())){
            String[] idArray = componentIds.split(",");
            for (int i = 0; i < idArray.length; i++) {
                Integer componentId = Integer.parseInt(idArray[i]);
                //在组件--用户表中加入记录
                cmsComponentUserPo.setUserId(cmsUserPo.getId());
                cmsComponentUserPo.setComponentId(componentId);
                cmsComponentUserService.save(cmsComponentUserPo);
                //在组件——管理员中间表中加入记录
                cmsComponentManagerPo.setManagerId(managerId);
                cmsComponentManagerPo.setComponentId(componentId);
                cmsComponentManagerService.save(cmsComponentManagerPo);
            }
        }















    }
}

